package com.binfast.app.admin.services.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.SecureUtil;
import com.binfast.app.admin.services.LoginServ;
import com.binfast.bean.dto.login.LoginDTO;
import com.binfast.bean.dto.login.UserInfoDTO;
import com.binfast.bean.model.auth.AdminMenu;
import com.binfast.bean.model.auth.AdminUser;
import com.binfast.bean.params.login.LoginDetailParam;
import com.binfast.common.constant.enums.ResultCode;
import com.binfast.common.exception.PlatformException;
import com.binfast.common.util.JwtUtil;
import com.binfast.seed.base.BaseService;
import com.binfast.service.dao.auth.AdminMenuDao;
import com.binfast.service.dao.auth.AdminUserDao;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class LoginServImpl extends BaseService implements LoginServ {

    @Autowired
    AdminUserDao adminUserDao;

    @Autowired
    AdminMenuDao adminMenuDao;

    @Override
    public LoginDTO findOne(LoginDetailParam param) {
        if(param ==null)return  null;
        if(param.getUsername() == null || param.getPassword() == null) return  null;
        String password = SecureUtil.md5(param.getPassword());
        AdminUser adminUser = adminUserDao.findByUserNameAndPasswordAndDeleted(param.getUsername(),password);
        if(adminUser == null) throw new PlatformException("用户名或密码错误！");
        String token = JwtUtil.createJWT(adminUser.getId());
        return new LoginDTO(token);
    }

    @Override
    public UserInfoDTO getUserInfo(String token) {
//        String token = param.getToken();
        Claims claims = JwtUtil.parseJwtToken(token);
        Long userId = 0l;
        if(claims != null) userId = Convert.toLong(claims.get("userId"),0l);
        if(userId <= 0) throw new PlatformException(ResultCode.NO_LOGIN.getCode(),"验证用户信息失败");

        AdminUser adminUser = adminUserDao.findByIdAndDeleted(userId);
        if(adminUser == null) throw new PlatformException(ResultCode.NO_LOGIN.getCode(),"用户不存在或已注销");

        //拼装前端需要的异步路由
        List<AdminMenu> menuList = adminUserDao.findMenusByUser(adminUser.getId());
//        List<AdminMenu> adminMenuList = AdminMenu.generateTreeMenu(menuList);
//        List<MenuRouter> routerList = filterRouter(adminMenuList);
        List<String> routerList = generateAsyncRouter(menuList);

        return new UserInfoDTO(adminUser,routerList);
    }

    public List<String> generateAsyncRouter(List<AdminMenu> menus) {
        List<String> routerList = new ArrayList<>(menus.size()+16);
        menus.parallelStream().forEach(menu -> {
            routerList.add(menu.getPageName());
        });
        return routerList;
    }

//    public List<MenuRouter> filterRouter(List<AdminMenu> menus) {
//        List<MenuRouter> menuRouterList = new ArrayList<>();
//        if(menus == null || menus.size() == 0) return menuRouterList;
//        menus.stream().forEach(menu -> {
//            MenuRouter menuRouter = new MenuRouter();
//            menuRouter.setPath(menu.getPath());
//            menuRouter.setComponent(menu.getComponent());
//            menuRouter.setRoles("admin");
//            if(StrUtil.isNotBlank(menu.getName()))menuRouter.setName(menu.getPageName());
//            if(StrUtil.isNotBlank(menu.getRedirect()))menuRouter.setRedirect(menu.getRedirect());
//            if(Convert.toBool(menu.getIsHidden(),false))menuRouter.setHidden(menu.getIsHidden());
//            MetaRouter metaRouter = new MetaRouter();
//            metaRouter.setTitle(menu.getTitle());
//            metaRouter.setIcon(menu.getIconCls());
//            if(Convert.toBool(menu.getNoCache(),false))metaRouter.setNoCache(menu.getNoCache());
//
//            menuRouter.setMeta(metaRouter);
//            if(menu.getMenuList() != null && menu.getMenuList().size() > 0) {
//                menuRouter.setChildren(filterRouter(menu.getMenuList()));
//            }
//            menuRouterList.add(menuRouter);
//        });
//        return menuRouterList;
//    }
}
